/* GUI CONVERTED from gppccal.p (converter v1.75) Sun Aug 13 18:33:22 2000 */
/* gppccal.p - CALCULATE PRICE                                          */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/*F0PN*/ /*V8:ConvertMode=Maintenance                                   */
/* REVISION: 7.4     LAST MODIFIED: 09/28/93    BY: cdt *H086**/
/* REVISION: 7.4     LAST MODIFIED: 05/21/94    BY: qzl *H374**/
/* REVISION: 7.4     LAST MODIFIED: 06/16/94    BY: qzl *H390**/
/* REVISION: 7.4     LAST MODIFIED: 07/12/94    BY: bcm *H439**/
/* REVISION: 7.4     LAST MODIFIED: 07/18/94    BY: bcm *H441**/
/* REVISION: 7.4     LAST MODIFIED: 09/09/94    BY: afs *H510**/
/* REVISION: 7.4     LAST MODIFIED: 08/25/95    BY: jym *G0TW**/
/* REVISION: 8.5     LAST MODIFIED: 11/12/98    BY: *J34G* Alfred Tan   */
/* REVISION: 9.1     LAST MODIFIED: 04/25/00    BY: *N0CD* Santosh Rao  */
/* $Revision: 1.11 $      BY: Pat Pigatti  Date: 07/7/00  ECO: *N0FF*       */
/* REVISION: 9.1     LAST MODIFIED: 08/13/00    BY: *N0KS* myb              */
/* REVISION: EB2 SP3 LAST MODIFIED: 12/05/03    BY: Kaine Zhang  *hk-s-026*   */

/***********************************************************************
*  This subroutine looks for a price list master based on quantity
*  and other passed in information.
*
*  Successive finds are attempted to locate a matching price list:
*      1)  price list, currency, effective date, item, um;
*      2)  price list, currency, effective date, item;
*      3)  price list, currency, effective date, product line, um;
*      4)  price list, currency, effective date, product line;
*      5)  price list, currency, effective date, um;
*      6)  price list, currency, effective date.
*
*  The quantities and prices are in the unit of measure on the price
*  list.  If no unit of measure is specified, the stocking unit of
*  measure is used.  NOTE: THIS IS A CHANGE FROM VERSION 7.3 (Patch H510).
*
*  If no price list entry is found or the ordered quantity is less
*  then the minimum price list quantity, the price defaults to the
*  price in the item master (pt_price). The minimum quantities on
*  the price list are in stocking unit of measure, not price list
*  unit of measure.
*
*  An exact item/um price list match may be required.
*  If the shared variable match_pt_um is passed in as true, then it will
*  will be returned as a flag indicating whether a matching price list
*  was found.  This variable will normally be set based on control file
*  variables.
*
************************************************************************
*H510 - added clarification on qty UM below
*
* PARAMETERS
*    part        -  item to be priced
*    pcqty       -  item quantity in ORDERED unit of measure
*    um          -  ORDERED unit of measure
*    umconv      -  conversion of ordered UM to STOCK UM
*    curr        -  order currency
*    price_list  -  discount table name
*    eff_date    -  effective date for price look-up
*    cost        -  item cost in ORDERED UM
*    match_pt_um -  indicates whether item/um match is required
*                   (returned true if valid match is found)
*    list_price  -  item list price, used to calculate net price
*H441*              (list_price is no longer changed by this routine)
*    disc_pct    -  calculated % between list_price and net_price
*                   (assuming list_price <> 0)
*    net_price   -  final price based on discount table entry
*                   (net_price is unchanged if no entry found)
*    pc_recno    -  record id of price table entry used  *H510*
*
***********************************************************************/


     define input        parameter part        like pc_part.
     define input        parameter pcqty       as decimal.
     define input        parameter um          like pc_um.
     define input        parameter umconv      like sod_um_conv.
     define input        parameter curr        like pc_curr.
     define input        parameter price_list  like pc_list.
     define input        parameter eff_date    like pc_start.
     define input        parameter cost        as decimal.
     define input        parameter match_pt_um as logical.
/*G0TW*/ define input        parameter supplier_disc like po_disc_pct.
/*hk-s-026*/  define input        parameter chrPodSitea as character.
     define input-output parameter list_price  like pt_price.
     define output       parameter disc_pct    as decimal.
     define input-output parameter net_price   like pt_price.
/*H510*/ define output       parameter pc_recno    as recid.

     define              variable  jk          as integer.
/*H510** define              variable  ord_qty     as integer.  **/

/*N0CD** find pt_mstr where pt_part = part no-lock no-error. **/

/*N0CD*/ for first pt_mstr
/*N0CD*/     fields(pt_part pt_prod_line)
/*N0CD*/     where pt_part = part no-lock:
/*N0CD*/ end. /*FOR FIRST PT_MSTR */

/*H390*  if not available pt_mstr then leave.   */

     assign
        disc_pct   = 0
/*H510*/    pc_recno   = 0.

/*H510** ord_qty = pcqty / umconv. **/

     /* FIND PRICE LIST FOR PART AND UM */

/*N0CD** BEGIN DELETE **
 *       find last pc_mstr where pc_list = price_list
 *               and pc_part = part
 *               and pc_um = um
 *               and (pc_start <= eff_date or pc_start = ?)
 *               and (pc_expire >= eff_date or pc_expire = ?)
 *               and pc_curr = curr
 * /*H510*/                     and pc_amt_type <> "L"
 *       no-lock no-error.
 *N0CD** END DELETE **/

/*N0CD*/ for last pc_mstr
/*N0CD*/     fields(pc_amt pc_amt_type pc_curr pc_expire pc_list
/*N0CD*/            pc_min_qty pc_part pc_prod_line pc_start pc_um)
/*N0CD*/     where pc_list      = price_list                  and
/*N0CD*/           pc_part      = part                        and
/*N0CD*/           pc_um        = um                          and
/*N0CD*/           (pc_start    <= eff_date or pc_start = ?)  and
/*N0CD*/           (pc_expire   >= eff_date or pc_expire = ?) and
/*N0CD*/           pc_curr      = curr                        and
/*hk-s-026*/	   pc_user1 = chrPodSitea	and
/*N0CD*/           pc_amt_type  <> "L" no-lock:
/*N0CD*/ end. /* FOR LAST PC_MSTR */

     /* If an item/um price list is required and we didn't find one, */
     /* then we can stop now and go home.                            */
     if match_pt_um then do:
        match_pt_um = (available pc_mstr).
        if not match_pt_um then leave.
     end.

     /* FIND PRICE LIST FOR PART REGARDLESS OF UM */
     if not available pc_mstr then

/*N0CD** BEGIN DELETE **
 *       find last pc_mstr where pc_list = price_list
 *                and pc_part = part
 *                and pc_um = ""
 *                and (pc_start <= eff_date or pc_start = ?)
 *                and (pc_expire >= eff_date or pc_expire = ?)
 *                and pc_curr = curr
 * /*H510*/       and pc_amt_type <> "L"
 *       no-lock no-error.
 *N0CD** END DELETE **/

/*N0CD*/ for last pc_mstr
/*N0CD*/     fields(pc_amt pc_amt_type pc_curr pc_expire pc_list
/*N0CD*/            pc_min_qty pc_part pc_prod_line pc_start pc_um)
/*N0CD*/     where pc_list      = price_list                  and
/*N0CD*/           pc_part      = part                        and
/*N0CD*/           pc_um        = ""                          and
/*N0CD*/           (pc_start    <= eff_date or pc_start = ?)  and
/*N0CD*/           (pc_expire   >= eff_date or pc_expire = ?) and
/*N0CD*/           pc_curr      = curr                        and
/*hk-s-026*/	   pc_user1 = chrPodSitea	and
/*N0CD*/           pc_amt_type  <> "L" no-lock:
/*N0CD*/ end. /* FOR LAST PC_MSTR */

     /* FIND PRICE LIST FOR PRODUCT LINE AND UM */
     if not available pc_mstr and available pt_mstr then
/*N0CD** BEGIN DELETE **
 *       find last pc_mstr where pc_list = price_list
 *                and pc_part = ""
 *                and pc_prod_line = pt_prod_line
 *                and pc_um = um
 *                and (pc_start <= eff_date or pc_start = ?)
 *                and (pc_expire >= eff_date or pc_expire = ?)
 *                and pc_curr = curr
 * /*H510*/                      and pc_amt_type <> "L"
 *      no-lock no-error.
 *N0CD** END DELETE **/

/*N0CD*/ for last pc_mstr
/*N0CD*/     fields(pc_amt pc_amt_type pc_curr pc_expire pc_list
/*N0CD*/            pc_min_qty pc_part pc_prod_line pc_start pc_um)
/*N0CD*/     where pc_list      = price_list                  and
/*N0CD*/           pc_part      = ""                          and
/*N0CD*/           pc_prod_line = pt_prod_line                and
/*N0CD*/           pc_um        = um                          and
/*N0CD*/           (pc_start    <= eff_date or pc_start = ?)  and
/*N0CD*/           (pc_expire   >= eff_date or pc_expire = ?) and
/*N0CD*/           pc_curr      = curr                        and
/*hk-s-026*/	   pc_user1 = chrPodSitea	and
/*N0CD*/           pc_amt_type  <> "L" no-lock:
/*N0CD*/ end. /* FOR LAST PC_MSTR */

     /* FIND PRICE LIST FOR PRODUCT LINE REGARDLESS OF UM */
     if not available pc_mstr and available pt_mstr then
/*N0CD** BEGIN DELETE **
 *       find last pc_mstr where pc_list = price_list
 *                and pc_prod_line = pt_prod_line
 *                and pc_part = ""
 *                and pc_um = ""
 *                and (pc_start <= eff_date or pc_start = ?)
 *                and (pc_expire >= eff_date or pc_expire = ?)
 *                and pc_curr = curr
 * /*H510*/                      and pc_amt_type <> "L"
 *       no-lock no-error.
 *N0CD** END DELETE **/

/*N0CD*/ for last pc_mstr
/*N0CD*/     fields(pc_amt pc_amt_type pc_curr pc_expire pc_list
/*N0CD*/            pc_min_qty pc_part pc_prod_line pc_start pc_um)
/*N0CD*/     where pc_list      = price_list                  and
/*N0CD*/           pc_prod_line = pt_prod_line                and
/*N0CD*/           pc_part      = ""                          and
/*N0CD*/           pc_um        = ""                          and
/*N0CD*/           (pc_start    <= eff_date or pc_start = ?)  and
/*N0CD*/           (pc_expire   >= eff_date or pc_expire = ?) and
/*N0CD*/           pc_curr      = curr                        and
/*hk-s-026*/	   pc_user1 = chrPodSitea	and
/*N0CD*/           pc_amt_type  <> "L" no-lock:
/*N0CD*/ end. /* FOR LAST PC_MSTR */

     /* FIND PRICE LIST FOR ANY ITEM, MATCHING UM */
     if not available pc_mstr then
/*N0CD** BEGIN DELETE **
 *       find last pc_mstr where pc_list = price_list
 *                and pc_part = ""
 *                and pc_prod_line = ""
 *                and pc_um = um
 *                and (pc_start <= eff_date or pc_start = ?)
 *                and (pc_expire >= eff_date or pc_expire = ?)
 *                and pc_curr = curr
 * /*H510*/       and pc_amt_type <> "L"
 *       no-lock no-error.
 *N0CD** END DELETE **/

/*N0CD*/ for last pc_mstr
/*N0CD*/     fields(pc_amt pc_amt_type pc_curr pc_expire pc_list
/*N0CD*/            pc_min_qty pc_part pc_prod_line pc_start pc_um)
/*N0CD*/     where pc_list      = price_list                  and
/*N0CD*/           pc_part      = ""                          and
/*N0CD*/           pc_prod_line = ""                          and
/*N0CD*/           pc_um        = um                          and
/*N0CD*/           (pc_start    <= eff_date or pc_start = ?)  and
/*N0CD*/           (pc_expire   >= eff_date or pc_expire = ?) and
/*N0CD*/           pc_curr      = curr                        and
/*hk-s-026*/	   pc_user1 = chrPodSitea	and
/*N0CD*/           pc_amt_type <> "L" no-lock:
/*N0CD*/ end. /* FOR LAST PC_MSTR */

     /* FIND MATCHING PRICE LIST ONLY */
     if not available pc_mstr then

/*N0CD** BEGIN DELETE **
 *       find last pc_mstr where pc_list = price_list
 *                and pc_prod_line = ""
 *                and pc_part = ""
 *                and pc_um = ""
 *                and (pc_start <= eff_date or pc_start = ?)
 *                and (pc_expire >= eff_date or pc_expire = ?)
 *                and pc_curr = curr
 * /*H510*/                      and pc_amt_type <> "L"
 *       no-lock no-error.
 *N0CD** END DELETE **/

/*N0CD*/ for last pc_mstr
/*N0CD*/     fields(pc_amt pc_amt_type pc_curr pc_expire pc_list
/*N0CD*/            pc_min_qty pc_part pc_prod_line pc_start pc_um)
/*N0CD*/     where pc_list      = price_list                  and
/*N0CD*/           pc_prod_line = ""                          and
/*N0CD*/           pc_part      = ""                          and
/*N0CD*/           pc_um        = ""                          and
/*N0CD*/           (pc_start    <= eff_date or pc_start = ?)  and
/*N0CD*/           (pc_expire   >= eff_date or pc_expire = ?) and
/*N0CD*/           pc_curr      = curr                        and
/*hk-s-026*/	   pc_user1 = chrPodSitea	and
/*N0CD*/           pc_amt_type  <> "L" no-lock:
/*N0CD*/ end. /* FOR LAST PC_MSTR */

     if available pc_mstr then do:

        if pcqty < 0 then pcqty = pcqty * -1.

/*H510*/    /* Convert to stock um if we don't have a um match */
/*H510*/    if pc_um <> um then pcqty = pcqty * umconv.

        do jk = 1 to 15:
           if pc_min_qty[jk] > pcqty
        or (pc_min_qty[jk] = 0 and pc_amt[jk] = 0)
        then leave.
        end.
        jk = jk - 1.

        if jk > 0 then do:
           if pc_amt_type = "P" then do:
/*H510*/          if pc_um = um then
/*H510*/             net_price = pc_amt[jk].
/*H510*/          else
             net_price = pc_amt[jk] * umconv.
           end.
           else if pc_amt_type = "D" then do:
/*N0CD*/      assign
                 disc_pct   = pc_amt[jk]
                 net_price  = list_price * (1 - (disc_pct / 100)).
           end.
           else if pc_amt_type = "M" then do:
          net_price  = cost * (1 + (pc_amt[jk] / 100)).
/*G0TW*/          if supplier_disc <> 0 then
/*G0TW*/            net_price = cost * (1 + (pc_amt[jk] / 100)) *
/*G0TW*/              ( (100 - supplier_disc) / 100).
           end.
        end.

/*H441**    if list_price = 0 then list_price = net_price. **/
        if disc_pct   =  0
         and list_price <> 0
         and net_price  <> list_price
/*G0TW*      then disc_pct = (1 - (net_price / list_price)) * 100. */
/*G0TW*/     then do:
/*G0TW*/       if pc_amt_type = "p" and supplier_disc <> 0 then
/*G0TW*/         net_price = net_price * (( 100 - supplier_disc) / 100).
/*G0TW*/       disc_pct = (1 - (net_price / list_price)) * 100.
/*G0TW*/     end.
/*H374*/    /* If the quantity ordered is below the discount table  */
/*H374*/    /* quantity range, the percentage should set to zero,   */
/*H374*/    /* otherwise the extended cost would be zero.           */
/*H374*/    if jk <= 0 then disc_pct = 0.

/*H510*/    pc_recno = recid(pc_mstr).

     end.  /* available pc_mstr */
/*N0FF *** Begin Add Block */
     else
        assign
           disc_pct = supplier_disc.
/*N0FF *** End Add Block */
